#include "airplane.h"
#include "ui_airplane.h"
#include<qrect.h>
#include<setting.h>
#include"QIcon"
#include<QPainter>
#include<QMouseEvent>
Airplane::Airplane(QWidget *parent)
    : QWidget(parent)
{
    //调用初始化场景
    initScene();

    //启动游戏
    playGame();
}

Airplane::~Airplane()
{

}

void Airplane::initScene()
{
   //设置窗口固定尺寸
    setFixedSize(GAME_WIDTH,GAME_HEIGHT);

    //设置标题
    setWindowTitle(GAME_TITLE);

    //加载图标
    setWindowIcon(QIcon(":/myimage/images/hero.png"));

    //定时器设置
    m_Timer.setInterval(GAME_RATE);
    //敌机出场时间间隔 初始化
       m_recorder=0;

       //随机数种子
       srand((unsigned int)time(NULL));
}
void Airplane::playGame(){
    //启动计时器
    m_Timer.start();

    //监听定时器发送的信号
    connect(&m_Timer,&QTimer::timeout,[=](){
        //更新游戏中元素的坐标
        updatePosition();
        //绘制到屏幕中
        update();
        //敌机出场
                enemytoscene();
    });
}
void Airplane::updatePosition(){
    //更新地图坐标
    m_map.mapPosition();

    //发射子弹
       m_hero.shoot();
        //计算所有非空闲子弹的当前坐标
        for(int i=0;i<BULLET_NUM;i++){
            //如果非空闲，计算发射位置
            if(m_hero.m_bullets[i].m_Free==false){
                m_hero.m_bullets[i].updatePosition();
            }
        }
        //敌机出场
            for(int i=0;i<ENEMY_NUM;i++){
                if(m_enemys[i].m_Free==false){
                    m_enemys[i].updatePosition();
                }
            }

        //测试子弹
       // temp_Bullet.m_Free=false;
       //temp_Bullet.updatePosition();
    }
void Airplane::paintEvent(QPaintEvent *){
    QPainter painter(this);

    //绘制地图
    painter.drawPixmap(0,m_map.m_map1_posY,m_map.m_map1);
    painter.drawPixmap(0,m_map.m_map2_posY,m_map.m_map2);

    //绘制英雄飞机
    painter.drawPixmap(m_hero.m_X,m_hero.m_Y,m_hero.m_Plane);
    //绘制子弹
    for(int i=0;i<BULLET_NUM;i++){
            //如果非空闲，绘制
            if(m_hero.m_bullets[i].m_Free==false){
            painter.drawPixmap(m_hero.m_bullets[i].m_X,m_hero.m_bullets[i].m_Y,m_hero.m_bullets[i].m_Bullet);
            }
        }
    //绘制敌机
        for(int i=0;i<ENEMY_NUM;i++){
            if(m_enemys[i].m_Free==false){
                painter.drawPixmap(m_enemys[i].m_X,m_enemys[i].m_Y,m_enemys[i].m_enemy);
            }
        }

    //测试子弹
      // painter.drawPixmap(temp_Bullet.m_X,temp_Bullet.m_Y,temp_Bullet.m_Bullet);
}
void Airplane::mouseMoveEvent(QMouseEvent *event){
    int x=event->x()-m_hero.m_Rect.width()*0.5;
    int y=event->y()-m_hero.m_Rect.height()*0.5;

    //边界检测
    if(x<=0){
        x=0;
    }
    if(x>=GAME_WIDTH-m_hero.m_Rect.width()){
        x=GAME_WIDTH-m_hero.m_Rect.width();
    }
    if(y<=0){
        y=0;
    }
    if(y>=GAME_HEIGHT-m_hero.m_Rect.height()){
        y=GAME_HEIGHT-m_hero.m_Rect.height();
    }

    m_hero.setPosition(x,y);
}
void Airplane::enemytoscene()
{
   m_recorder++;
   //未达到出场间隔，直接return
   if(m_recorder<ENEMY_INTERVAL){
       return;
   }
   m_recorder=0;

   for(int i=0;i<ENEMY_NUM;i++){
       //如果是空闲敌机 出场
       if(m_enemys[i].m_Free){
           m_enemys[i].m_Free=false;

           //坐标
           m_enemys[i].m_X=rand()%(GAME_WIDTH-m_enemys[i].m_Rect.width());
           m_enemys[i].m_Y=-m_enemys[i].m_Rect.height();
           break;
       }
   }
}
